package com.zcx.qshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.adapter.CommodityDetailAdapter;
import com.zcx.qshop.conn.Conn;
import com.zcx.qshop.conn.JsonCollectaddAsyGet;
import com.zcx.qshop.conn.JsonEvaluatelistAsyGet;
import com.zcx.qshop.conn.JsonGoodinfoAsyGet;
import com.zcx.qshop.db.QSDataBase;
import com.zcx.qshop.entity.Good;
import com.zcx.qshop.view.CacheWebView;
import com.zcx.qshop.view.CommodityDetailBannerView;
import com.zcx.qshop.view.CommodityDetailBar;

/**
 * Created by Administrator on 2015/8/23 0023.
 */
public class CommodityDetailActivity extends QSActivity implements View.OnClickListener{

    public static OnCommodityDetailChangeListener onCommodityDetailChangeListener;

    @BoundView(R.id.commodity_detail_back) private View back;

    @BoundView(R.id.commodity_detail_banner_view) private CommodityDetailBannerView commodityDetailBannerView;

    @BoundView(R.id.commodity_detail_title) private TextView title;

    @BoundView(R.id.commodity_detail_scroll) private ScrollView scrollView;

    @BoundView(R.id.commodity_detail_price) private TextView price;

    @BoundView(R.id.commodity_detail_unit) private TextView unit;

    @BoundView(R.id.commodity_detail_number) private TextView number;

    @BoundView(R.id.commodity_detail_description) private TextView description;

    @BoundView(R.id.commodity_detail_attribute) private View attribute;

    @BoundView(R.id.commodity_detail_bar) private CommodityDetailBar commodityDetailBar;

    @BoundView(R.id.commodity_detail_web_layout) private View webLayout;

    @BoundView(R.id.commodity_detail_web_open) private View webOpen;

    @BoundView(R.id.commodity_detail_web_backgound) private View webBackgound;

    @BoundView(R.id.commodity_detail_web_view) private CacheWebView cacheWebView;

    @BoundView(R.id.commodity_detail_list_view) private ListView listView;

    @BoundView(R.id.commodity_detail_collect) private ViewGroup collect;

    @BoundView(R.id.commodity_detail_shop_car) private View chopCar;

    @BoundView(R.id.commodity_detail_add_shop_car) private View addShopCar;

    @BoundView(R.id.commodity_detail_buy) private View buy;

    private JsonGoodinfoAsyGet.Info info;

    private Good good;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_commodity_detail);

        commodityDetailBar.setOnItemClickListener(new CommodityDetailBar.OnItemClickListener() {

            @Override
            public void onWebClick() {

                webLayout.setVisibility(View.VISIBLE);

                webBackgound.setVisibility(cacheWebView.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);

                listView.setVisibility(View.GONE);

            }

            @Override
            public void onListClick() {

                webLayout.setVisibility(View.GONE);

                webBackgound.setVisibility(View.GONE);

                listView.setVisibility(View.VISIBLE);

            }

        });

        good = (Good) getIntent().getSerializableExtra("Good");

        back.setOnClickListener(this);

        chopCar.setOnClickListener(this);

        onCommodityDetailChangeListener = new OnCommodityDetailChangeListener() {

            JsonGoodinfoAsyGet jsonGoodinfoAsyGet = new JsonGoodinfoAsyGet("", good.pid, new AsyCallBack<JsonGoodinfoAsyGet.Info>(){

                @Override
                public void onSuccess(String toast, int type, final JsonGoodinfoAsyGet.Info info) throws Exception {

                    title.setText((CommodityDetailActivity.this.info = info).title);

                    commodityDetailBannerView.setItems(info.picurls);

                    commodityDetailBannerView.setOnItemClickListener(new CommodityDetailBannerView.OnItemClickListener() {

                        @Override
                        public void onItemClick(JsonGoodinfoAsyGet.Info.Picurl banner) {

                            startVerifyActivity(PicurlDetailActivity.class, new Intent().putExtra("Info", info));

                        }

                    });

                    commodityDetailBannerView.setOnTouchListener(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {

                            if (event.getAction() == MotionEvent.ACTION_UP) {

                                scrollView.requestDisallowInterceptTouchEvent(false);

                            } else {

                                scrollView.requestDisallowInterceptTouchEvent(true);

                            }

                            return false;

                        }

                    });

                    price.setText("￥" + info.price);

                    unit.setText("元/" + info.unit);

                    number.setText("销量 " + info.number);

                    description.setText(Html.fromHtml(info.description));

                    description.setVisibility(info.description.equals("") ? View.GONE : View.VISIBLE);

                    commodityDetailBar.setCount(info.renumber);

                    addShopCar.setVisibility(info.type == null || info.type.equals("") || info.type.equals("1") ? View.VISIBLE : View.INVISIBLE);

                    attribute.setOnClickListener(CommodityDetailActivity.this);

                    webOpen.setOnClickListener(CommodityDetailActivity.this);

                    collect.setOnClickListener(CommodityDetailActivity.this);

                    addShopCar.setOnClickListener(CommodityDetailActivity.this);

                    buy.setOnClickListener(CommodityDetailActivity.this);

                    ((ImageView)collect.getChildAt(0)).setImageResource(info.isshoucang.equals("0") ? R.mipmap.commodity_detail_collection : R.mipmap.commodity_detail_collection_select);

                    good.price = info.price;

                    good.unit = info.unit;

                    QSDataBase.HistoryTable.insert(QSApplication.QSPreferences.readSchool().sid, good);

                    cacheWebView.loadUrl(Conn.SERVICE_URL + "json_goodcontent.php?gid=" + good.pid);

                    new JsonEvaluatelistAsyGet(good.pid, new AsyCallBack<JsonEvaluatelistAsyGet.Info>(){

                        @Override
                        public void onSuccess(String toast, int type, JsonEvaluatelistAsyGet.Info info) throws Exception {

                            listView.setAdapter(new CommodityDetailAdapter(CommodityDetailActivity.this, info.evaluates));

                        }

                    }).execute(CommodityDetailActivity.this);

                }

                @Override
                public void onFail(String toast, int type) throws Exception {

                    UtilToast.show(CommodityDetailActivity.this, "该商品不存在");

                    finish();

                }

            });

            @Override
            public void onCommodityDetailChange() {

                jsonGoodinfoAsyGet.uid = QSApplication.QSPreferences.readUid();

                jsonGoodinfoAsyGet.execute(CommodityDetailActivity.this);

            }

        };

        onCommodityDetailChangeListener.onCommodityDetailChange();

    }

    @Override
    public void onClick(View v) {

        switch(v.getId()){

            case R.id.commodity_detail_back:

                finish();

                break;

            case R.id.commodity_detail_buy:
            case R.id.commodity_detail_attribute:
            case R.id.commodity_detail_add_shop_car:

                QSApplication.ShoppingAction.showAttribute(this, good.pid);

                break;

            case R.id.commodity_detail_collect:

                if(QSApplication.LoginAction.isLogin()){

                    if(info.isshoucang.equals("1")){

                        UtilToast.show(this, "已收藏");

                    }else{

                        new JsonCollectaddAsyGet(QSApplication.QSPreferences.readUid(), good.pid, new AsyCallBack(){

                            @Override
                            public void onSuccess(String toast, int type, Object o) throws Exception {

                                info.isshoucang = "1";

                                ((ImageView)collect.getChildAt(0)).setImageResource(R.mipmap.commodity_detail_collection_select);

                            }

                            @Override
                            public void onEnd(String toast, int type) throws Exception {

                                UtilToast.show(CommodityDetailActivity.this, toast);

                            }
                        }).execute(this);

                    }

                }else{

                    startVerifyActivity(LoginActivity.class);

                }

                break;

            case R.id.commodity_detail_shop_car:

                QSApplication.INSTANCE.finishActivity(ActivitvActivity.class, ThreeListActivity.class, TwoListActivity.class, CollectionActivity.class, HistoryActivity.class, SearchResultActivity.class, SearchActivity.class);

                if(NavigationActivity.onFragmentChangeListener != null){

                    NavigationActivity.onFragmentChangeListener.onFragmentToShopCar();

                }

                finish();

                break;

            case R.id.commodity_detail_web_open:

                v.setVisibility(View.GONE);

                webBackgound.setVisibility(View.GONE);

                cacheWebView.setVisibility(View.VISIBLE);

                cacheWebView.loadUrl(Conn.SERVICE_URL + "json_goodcontent.php?gid=" + good.pid);

                break;

        }

    }

    @Override
    protected void onDestroy() {

        onCommodityDetailChangeListener = null;

        super.onDestroy();

    }

    public interface OnCommodityDetailChangeListener{

        void onCommodityDetailChange();

    }

}